//
//  WWYCollectionVideoCell.m
//  Pianke
//
//  Created by Daydream on 15/12/23.
//  Copyright © 2015年 dada. All rights reserved.
//

#import "WWYCollectionVideoCell.h"
#import "WWYVideoModel.h"


@interface WWYCollectionVideoCell ()

@property (nonatomic, strong) UIImageView *backImage;

@property (nonatomic, strong) UIImageView *playImage;

@property (nonatomic, strong) UILabel *titel;

@property (nonatomic, strong) UIView *line;

@property (nonatomic, strong) UILabel *sortAndDuration;

@property (nonatomic, strong) UILabel *video_description;

@property (nonatomic ,strong) UIImageView *LableBackView;


@end

@implementation WWYCollectionVideoCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self makeItem];
    }
    return self;
}

- (void)makeItem{
    //上层大背景
    self.backImage = [[UIImageView alloc] init];
    self.backImage.backgroundColor = [UIColor grayColor];
    [self.contentView addSubview:self.backImage];
    
    //上层点击播放
    self.backImage.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(PlayAction:)];
    [self.backImage addGestureRecognizer:tap];
    
    //下层小背景
    self.LableBackView = [[UIImageView alloc] init];
    self.LableBackView.backgroundColor = [UIColor darkGrayColor];
    [self.contentView addSubview:self.LableBackView];
    
    [self.backImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@0);
        make.top.mas_equalTo(@0);
        make.right.mas_equalTo(@0);
        make.height.mas_equalTo(self.contentView.frame.size.height * 2 / 3);
    }];
    [self.LableBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.backImage.mas_bottom).offset(1);
        make.bottom.mas_equalTo(@0);
        make.left.mas_equalTo(@0);
        make.right.mas_equalTo(@0);
    }];
    
    //标题
    self.titel = [[UILabel alloc] init];
    self.titel.textColor = [UIColor whiteColor];
    [self.LableBackView addSubview:self.titel];
    
    //下划线
    self.line = [[UIView alloc] init];
    self.line.backgroundColor = [UIColor whiteColor];
    [self.LableBackView addSubview:self.line];
    
    //分类,时长
    self.sortAndDuration = [[UILabel alloc] init];
    [self.LableBackView addSubview:self.sortAndDuration];
    
    //描述
    self.video_description = [[UILabel alloc] init];
    self.video_description.textColor = [UIColor whiteColor];
    self.video_description.font = [UIFont systemFontOfSize:12];
    self.video_description.numberOfLines = 0;
    [self.LableBackView addSubview:self.video_description];
}


- (void)setModel:(WWYVideoModel *)model{
    if (_model != model) {
        _model = model;
        //masonry真吭
        NSLog(@"self %f", self.LableBackView.frame.size.width);
        //标题约束
        [self.titel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(@20);
            make.top.mas_equalTo(@10);
            make.right.mas_equalTo(@20);
            make.height.mas_equalTo(@20);
        }];
        self.titel.text = self.model.title;
        
        //标题下划线约束
        [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.titel.mas_bottom).offset(5);
            make.left.mas_equalTo(self.titel.mas_left);
            make.height.mas_equalTo(@1);
            make.right.mas_equalTo(-20);
        }];
        
        //上背景赋值
        [self.backImage sd_setImageWithURL:[NSURL URLWithString:self.model.backgorund] placeholderImage:[UIImage imageNamed:@"zanwei"]];
        //self.backImage.layer.masksToBounds = YES;
        // 缩放模式
        self.backImage.contentMode = UIViewContentModeScaleAspectFill;
        self.backImage.clipsToBounds = YES;
        
        //下背景赋值
        [self.LableBackView sd_setImageWithURL:[NSURL URLWithString:self.model.LabelBack]];
        
        //时长种类
        [self.sortAndDuration mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.titel.mas_bottom).offset(10);
            make.left.mas_equalTo(self.titel);
            make.size.mas_equalTo(CGSizeMake(100, 20));
        }];
        self.sortAndDuration.textColor = [UIColor whiteColor];
        NSString *sort = self.model.category;
        NSString *min =  [NSString stringWithFormat:@"%ld", self.model.duration / 60];
        NSString *sec = [NSString stringWithFormat:@"%ld", self.model.duration % 60];
        self.sortAndDuration.text = [NSString stringWithFormat:@"# %@ | %@'%@ ", sort, min, sec];
        
        //自适应简介
        //介绍(一直刷新);
        //自适应有点问题..加大一号字体可以解决...为什么
        CGSize descriptionSize = [WWY_AppTools SizeForCommitSize:CGSizeMake(self.contentView.frame.size.width - 40, 100000000) andFont:[UIFont systemFontOfSize:13] andContent:_model.video_description];
        
        //第一次进来设置拘束
        if (self.video_description.text == nil){
            [self.video_description mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(self.sortAndDuration.mas_bottom);
                make.left.mas_equalTo(self.sortAndDuration);
                make.size.mas_equalTo(descriptionSize);
                
            }];
        //后来都是刷新约束
        }else{
            [self.video_description mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(self.sortAndDuration.mas_bottom);
                make.left.mas_equalTo(self.sortAndDuration);
                make.size.mas_equalTo(descriptionSize);
            }];
        }
        self.video_description.text = self.model.video_description;
    }
}

- (void)PlayAction:(UITapGestureRecognizer *)tap{
    //高清普清俩个在数组里
    [self.delegate WWYCollectionVideoCellModel:self.model];
}

@end
